using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using DiscMaster.Web.Models;

namespace DiscMaster.Web.Areas.Matches.Controllers
{
    public class MatchesController : Controller
    {
        private IDiscMasterRepository repository;

        public MatchesController()
            : this(new DiscMasterRepository()){
        }

        public MatchesController(IDiscMasterRepository controllerrepository)
        {
            repository = controllerrepository;
        }
        
        //
        // GET: /Matches/

        [OutputCache(CacheProfile = "ShortCache", VaryByParam = "none")]
        public ActionResult Index()
        {
            var matches = repository.FindAllMatches().OrderByDescending(m=>m.matchdate).ToList();
            if (matches.Count==0)
            {
                return View("NoData");
            }
            else
            {
                return View("Index", matches);
            }
        }

        //
        // GET: /Matches/Details/5

        [OutputCache(CacheProfile = "MediumCache", VaryByParam = "id")]
        public ActionResult Details(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                DiscMaster.Web.Models.Match match = repository.GetMatch(id);
                if (match == null)
                {
                    return View("NotFound");
                }
                return View("Details", match);
            }
            else
            {
                return View("NotFound");
            }
        }
        //
        // GET: /Matches/Results/5

        [OutputCache(CacheProfile = "LongCache", VaryByParam = "id")]
        public ActionResult Results(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                DiscMaster.Web.Models.Match match = repository.GetMatch(id);
                if (match.MatchResults == null)
                {
                    return View("NotFound");
                }
                return View("Results", match.MatchResults);
            }
            else
            {
                return View("NotFound");
            }
        }

        //
        // GET: /Matches/Statistics/5

        [OutputCache(CacheProfile = "LongCache", VaryByParam = "id")]
        public ActionResult Statistics(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                DiscMaster.Web.Models.Match match = repository.GetMatch(id);
                if (match.MatchResults == null)
                {
                    return View("NotFound");
                }
                return View("Statistics", match);
            }
            else
            {
                return View("NotFound");
            }
        }

        //
        // GET: /Matches/Create

        [Authorize()]
        public ActionResult Create()
        {
            var match = new DiscMaster.Web.Models.Match();
            return View(match);
        } 

        //
        // POST: /Matches/Create

        [Authorize()]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(DiscMaster.Web.Models.Match match)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    match.matchid = Guid.NewGuid();
                    UpdateModel(match);
                    match.createdby = repository.GetLoggedOnPlayer().UserId;
                    match.MatchPlayers.Add(new DiscMaster.Web.Models.MatchPlayer() { matchid = match.matchid, playerid = repository.GetLoggedOnPlayer().UserId });
                    repository.AddMatch(match);

                    repository.Save();

                    return RedirectToAction("Details", new { id = match.matchid });
                }
                catch (Exception ex)
                {
                    ViewData["Error"] = ex.Message;
                    return View(match);
                }
            }
            return View(match);
        }

        //
        // GET: /Matches/Edit/5

        [Authorize()]
        public ActionResult Edit(string id)
        {
            var match = repository.GetMatch(id);
            if (match==null)
            {
                return View("NotFound");
            }
            if (repository.GetLoggedOnPlayer().UserId!=match.createdby)
            {
                return View("OnlyOwner");
            }
            return View(match);
        }

        //
        // POST: /Matches/Edit/5

        [Authorize()]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, FormCollection collection)
        {
            var match = repository.GetMatch(id);
            if (match == null)
            {
                return View("NotFound");
            }
            if (repository.GetLoggedOnPlayer().UserId != match.createdby)
            {
                return View("OnlyOwner");
            }

            try
            {
                UpdateModel(match);

                repository.Save();

                return RedirectToAction("Details", new { id = match.matchid });
            }
            catch
            {
                return View(match);
            }
        }

        //
        // GET: /Matches/AddPlayer/5

        [Authorize()]
        public ActionResult AddPlayer(string id)
        {
            var match = repository.GetMatch(id);
            if (match == null)
            {
                return View("NotFound");
            }
            if (repository.GetLoggedOnPlayer().UserId != match.createdby)
            {
                return View("OnlyOwner");
            }
            return View(match);
        }

        //
        // POST: /Matches/AddPlayer/5

        [Authorize()]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddPlayer(string id, FormCollection collection)
        {
            var match = repository.GetMatch(id);
            if (match == null)
            {
                return View("NotFound");
            }
            if (repository.GetLoggedOnPlayer().UserId != match.createdby)
            {
                return View("OnlyOwner");
            }

            try
            {
                //UpdateModel(match);

                //repository.Save();

                return RedirectToAction("Details", new { id = match.matchid });
            }
            catch
            {
                return View(match);
            }
        }

        //
        // GET: /Matches/RemovePlayer/5

        [Authorize()]
        public ActionResult RemovePlayer(string id)
        {
            var match = repository.GetMatch(id);
            if (match == null)
            {
                return View("NotFound");
            }
            if (repository.GetLoggedOnPlayer().UserId != match.createdby)
            {
                return View("OnlyOwner");
            }
            return View(match);
        }

        //
        // POST: /Matches/RemovePlayer/5

        [Authorize()]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult RemovePlayer(string id, FormCollection collection)
        {
            var match = repository.GetMatch(id);
            if (match == null)
            {
                return View("NotFound");
            }
            if (repository.GetLoggedOnPlayer().UserId != match.createdby)
            {
                return View("OnlyOwner");
            }

            try
            {
                //UpdateModel(match);

                //repository.Save();

                return RedirectToAction("Details", new { id = match.matchid });
            }
            catch
            {
                return View(match);
            }
        }
    }
}
